package cn.stylefeng.guns.modular.weixin.controller;

import org.springframework.stereotype.*;
import me.chanjar.weixin.mp.api.*;
import cn.stylefeng.guns.modular.recy.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.stylefeng.guns.modular.system.service.*;
import javax.servlet.http.*;
import org.springframework.ui.*;
import me.chanjar.weixin.common.error.*;
import me.chanjar.weixin.mp.bean.result.*;
import org.apache.ibatis.annotations.*;
import me.chanjar.weixin.common.bean.*;
import java.net.*;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping({ "/wxmp/redirect/{appid}" })
public class WxRedirectController
{
    private WxMpService wxService;
    private String PREFIX;
    @Autowired
    private WechatUserService wechatUserService;
    @Autowired
    private DictService dictService;
    @Autowired
    private HttpServletRequest request;
    
    @Autowired
    public WxRedirectController(final WxMpService wxService) {
        this.PREFIX = "/weixin/";
        this.wxService = wxService;
    }
    
    @RequestMapping({ "/greet" })
    public String greetUser(@PathVariable final String appid, @RequestParam final String code, final ModelMap map) {
        if (!this.wxService.switchover(appid)) {
            throw new IllegalArgumentException(String.format("\u672a\u627e\u5230\u5bf9\u5e94appid=[%s]\u7684\u914d\u7f6e\uff0c\u8bf7\u6838\u5b9e\uff01", appid));
        }
        try {
            final WxMpOAuth2AccessToken accessToken = this.wxService.oauth2getAccessToken(code);
            final WxMpUser user = this.wxService.oauth2getUserInfo(accessToken, (String)null);
            map.put("user", user);
        }
        catch (WxErrorException e) {
            e.printStackTrace();
        }
        return this.PREFIX + "greet_user.html";
    }
    
    @ResponseBody
    @RequestMapping({ "/jsSign" })
    public WxJsapiSignature jsSign(@Param("url") String url) throws Exception {
        url = URLDecoder.decode(url, "UTF-8");
        System.out.println(this.wxService.getJsapiTicket());
        final WxJsapiSignature signature = this.wxService.createJsapiSignature(url);
        System.out.println(signature + "==========================================================");
        return signature;
    }
}
